package com.zs.blog.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.zs.blog.model.ResponseResult;
import com.zs.blog.model.dto.CategoryDTO;
import com.zs.blog.model.dto.CategoryEditDTO;
import com.zs.blog.model.dto.CategorySearchDTO;
import com.zs.blog.model.vo.CategoryEditVO;
import com.zs.blog.model.vo.CategoryListVO;
import com.zs.blog.model.vo.PageInfoVO;
import com.zs.blog.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: CategoryController
 * @Author zs
 * @Package com.zs.blog.controller
 * @Date 2024/10/20 16:15
 * @description: 分类接口层
 */
@RestController
@Tag(name = "分类相关接口")
public class CategoryController {

    public ICategoryService categoryService;

    @Autowired
    public CategoryController(ICategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/admin/category/list")
    @Operation(summary = "获取分页的分类列表")
    public ResponseResult<PageInfoVO<CategoryListVO>> listCategoryWithPagination(Integer pageNum, Integer pageSize) {
        PageInfoVO<CategoryListVO> listAllCategory = categoryService.listCategoryWithPagination(pageNum, pageSize);
        return ResponseResult.success(listAllCategory);
    }

    @PostMapping("/admin/category/search")
    @Operation(summary = "通过搜索条件获取分页的分类列表")
    public ResponseResult<PageInfoVO<CategoryListVO>> listCategoryBySearchCondition(@RequestBody CategorySearchDTO categorySearchDTO) {
        System.out.println(categorySearchDTO);
        PageInfoVO<CategoryListVO> listCategoryBySearchCondition = categoryService.listCategoryBySearchCondition(categorySearchDTO);
        return ResponseResult.success(listCategoryBySearchCondition);
    }

    @DeleteMapping("/admin/category/delete")
    @Operation(summary = "根据分类ID删除分类")
    public ResponseResult<?> deleteCategory(@RequestBody List<Integer> categoryIdList) {
        return categoryService.deleteCategoryByIds(categoryIdList);
    }

    @PutMapping("/admin/category/add")
    @Operation(summary = "新增分类")
    public ResponseResult<?> addCategory(@RequestBody CategoryEditDTO categoryEditDTO) {
        categoryService.addOrUpdateCategory(categoryEditDTO);
        return ResponseResult.success();
    }

    @PostMapping("/admin/category/update")
    @Operation(summary = "修改分类")
    public ResponseResult<?> updateCategory(@RequestBody CategoryEditDTO categoryEditDTO){
        categoryService.addOrUpdateCategory(categoryEditDTO);
        return ResponseResult.success();
    }

    @GetMapping("/admin/category/{categoryId}")
    @Operation(summary = "根据分类ID获取分类数据")
    public ResponseResult<CategoryEditVO> getCategoryByCategoryId(@PathVariable Long categoryId){
        CategoryEditVO category = categoryService.getCategoryByCategoryId(categoryId);
        return ResponseResult.success(category);
    }

    @GetMapping("/portal/category/list")
    @Operation(summary = "前台-获取分类列表")
    public ResponseResult<List<CategoryListVO>> listCategory(){
        List<CategoryListVO> listAllCategory = categoryService.listCategory();
        return ResponseResult.success(listAllCategory);
    }
 }
